How can I optimize a query with derived tables

join shbcf.ru